<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
    let data = {
        name:"后盾人",
        user:{
            name:"hdcms"
        },
        //添加数字
        arr:[]
    };

    //浅拷贝
    //  function copy(object){
    //      let res = {}
    //      for(const key in object){
    //          res[key] = object[key]
    //      }
    //    return res
    //  }

    //深拷贝
    // function copy(object){
    //      let res = {}
    //      for(const key in object){
    //          res[key] = typeof object[key] =="object"? 
    //          copy(object[key]) : object[key]
    //      }
    //      return res;
    //  }
    //  let hd =copy(data);
    //  hd.user.name="向军";
    //  console.log(JSON.stringify(hd, null, 2));
    //  console.log(JSON.stringify(data, null, 2));

     //数组深拷贝
     function copy(obj){
         let res =  obj instanceof Array ? [] : {};
         for(const [k,v] of Object.entries(obj)){
             res[k] = typeof v =="object"? copy(v) : v;
             
         }
         return res;
     }
     let hd =copy(data);
     hd.arr.push("45125fff") 
     console.log(JSON.stringify(hd, null, 2));
     console.log(JSON.stringify(data, null, 2));
</script>
    
</body>
</html>